package com.example.famhealing.schedule;

import java.util.Calendar;

import com.example.famhealing.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

//진료일정 추가 액티비티
public class AddDiagnoseActivity extends Activity implements OnClickListener {
	
	ImageButton prevButton, confirmButton, pickDate, setTime, offAlarm;
	
	RadioGroup rg;
	
	private int mHour, mMinute;
	
	String date = "";
	
	static final int ID_TIMEPICKER = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diagnose);
		
        //진료일정 분류 위한 스피너
        Spinner spinner = (Spinner) findViewById(R.id.spinner); 
        
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        		this, R.array.diagnose, android.R.layout.simple_spinner_item); 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //스피너 제목 설정
        spinner.setPrompt("분류");
        spinner.setAdapter(adapter); 
                  
        //스피너 아이템 선택 리스너
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
        	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
        		Toast.makeText(AddDiagnoseActivity.this
        				, "Spinner: position=" + position + " id=" + id 
        				, Toast.LENGTH_SHORT).show(); 
            } 
            public void onNothingSelected(AdapterView<?> parent) { 
            	Toast.makeText(AddDiagnoseActivity.this
            			, "Spinner: unselected"
                    	, Toast.LENGTH_SHORT).show(); 
            } 
        });
        
        prevButton = (ImageButton) findViewById(R.id.prevButton);
        confirmButton = (ImageButton) findViewById(R.id.confirmButton);
        pickDate = (ImageButton) findViewById(R.id.pickDate);
        setTime = (ImageButton) findViewById(R.id.setTime);
        offAlarm = (ImageButton) findViewById(R.id.offAlarm);
        
        prevButton.setOnClickListener(this);
        confirmButton.setOnClickListener(this);
        pickDate.setOnClickListener(this);
        setTime.setOnClickListener(this);
        offAlarm.setOnClickListener(this);
        
        //알림 시간 선택위한 라디오버튼(임시)
        rg = (RadioGroup) findViewById(R.id.alarmTime);
        
        date = CalendarPopup.date;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
			case R.id.prevButton :
				finish();
				break;
			case R.id.confirmButton :
				finish();
				break;
			case R.id.pickDate :
				
				//날짜 여러개 선택 가능하도록
		        CalendarMonthAdapter.multiSelect = true;
		        
		        //진료일정 추가할 날짜 선택위한 팝업창(달력) 띄우기
				Intent intent = new Intent(AddDiagnoseActivity.this, CalendarPopup.class);
				startActivity(intent);
				
				break;
			case R.id.setTime :
				//timepicker 띄우기
				final Calendar c = Calendar.getInstance();
				mHour = c.get(Calendar.HOUR_OF_DAY);
			    mMinute = c.get(Calendar.MINUTE);
			    showDialog(ID_TIMEPICKER);
				break;
			case R.id.offAlarm :
				break;
		}
	}
	
	public void getTime() {
		switch(rg.getCheckedRadioButtonId()) {
      		case R.id.oneHour:
      			break;
      		case R.id.thirty:
      			break;
      		case R.id.fifteen:
      			break;
      		case R.id.five:
      			break;
      		case R.id.at:
      			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch(id){
			case ID_TIMEPICKER:
				return new TimePickerDialog(this, myTimeSetListener, mHour, mMinute, false);
			default:
				return null;
	    
		}
	}
	  
	private TimePickerDialog.OnTimeSetListener myTimeSetListener 
		= new TimePickerDialog.OnTimeSetListener(){

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			String time = "Hour: " + String.valueOf(hourOfDay) + "\n"
					+ "Minute: " + String.valueOf(minute);
			Toast.makeText(AddDiagnoseActivity.this, time, Toast.LENGTH_SHORT).show();
		}
	};
	
	/*
    private class AddDiagnoseSchedule extends AsyncTask<Void, Void, String> {	

		@Override
		protected String doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			String url = "http:// 54.250.182.202:8080/famhealing/setDiagnoseSchedule.set?kind=1&time=날짜시간
					&phone_num=전화번호 &carcino=항암제&hospital=병원이름&type=진료형태
					&num=치료 횟수&period=치료사이의 기간&alarm=(on off)";
		
			String error = "";
			
			try {
				
				//Post				
				
				URL urladdr = new URL(url);
				
				//Parsing
				boolean inTitle1 = false;
		  			
		  		XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
		  			
		  		XmlPullParser parser = parserCreator.newPullParser(); 
		  		parser.setInput(urladdr.openStream(), "UTF-8");
		  		
		  		int parserEvent = parser.getEventType(); 
		  		
		  		String tag;
		  		
		  		while(parserEvent != XmlPullParser.END_DOCUMENT) {
		  			switch(parserEvent) {
		  			case XmlPullParser.START_DOCUMENT:
		  				break;
		  			case XmlPullParser.END_DOCUMENT:
		  				break;
		  			case XmlPullParser.START_TAG:
		  				tag = parser.getName();
		  				if(tag.equals("error")) {
		  					inTitle1 = true;
		  				}
		  				break;
		  			case XmlPullParser.TEXT:
		  				//tag = parser.getName();
		  				if(inTitle1) {
		  					error = parser.getText();
		  					inTitle1 = false;
		  				}
		  				break;
		  			case XmlPullParser.END_TAG:
		  				break;
		  			}
		  			parserEvent = parser.next();	
		  		}
		  			
		  	} catch (Exception e) {
		  			Log.e("dd", "Error in network call", e);
		  	}
			
			return error;
		}
		
		protected void onPostExecute(String error) {
			
			if(error.equals("0")){
				
				Toast.makeText(AddDiagnoseActivity.this, "일정 추가 성공!", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(AddDiagnoseActivity.this, "일정 추가 실패!", Toast.LENGTH_SHORT).show();
			}
			
			super.onPostExecute(error);
		}
	}
	*/

}
